﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace AnaViewer
{
    /// <summary>
    /// 头像 Hover 模式。
    /// </summary>
    public enum HoverMode
    {
        /// <summary>
        /// 无效果
        /// </summary>
        Nothing,
        /// <summary>
        /// 覆盖颜色。
        /// </summary>
        CoverColor,
        /// <summary>
        /// 黑白效果。
        /// </summary>
        BlankAndWhite,
    }

    /// <summary>
    /// 对呈现“项的作者”的过程进行封装。提供对 作者 的绘制（包括名字和头像）。
    /// </summary>
    public class ItemAuthorView : ViewBase
    {
        /// <summary>
        /// 无头像时，显示的图片。
        /// </summary>
        Image EmptyFace = Properties.Resources.person45;//default45;

        Rectangle realRect = Rectangle.Empty;
        public Rectangle RealRect
        {
            get { return realRect; }
        }
        
        private IItem displayItem;
        public IItem DisplayItem
        {
            get { return displayItem; }
            set { displayItem = value; this.Invalidate(); }
        }
        Color borderColor =Color.FromArgb(255, Color.White);
        /// <summary>
        /// 描边的颜色。
        /// </summary>
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }

        Color hoverBorderColor = Color.FromArgb(255, 199, 60);
        /// <summary>
        /// 描边的颜色。
        /// </summary>
        public Color HoverBorderColor
        {
            get { return hoverBorderColor; }
            set { hoverBorderColor = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        int borderWidth = 4;
        public int BorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        float flatness = 1.0F;
        /// <summary>
        /// 指定曲线展平的值。
        /// </summary>
        public float Flatness
        {
            get { return flatness; }
            set { flatness = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }

        public ItemAuthorView()
        {
            //名字的字体。
            base.Font = new Font("黑体", 16.0F);
        }
            

        /// <summary>
        /// 
        /// </summary>
        bool isActive = false;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive == value)
                    return;

                isActive = value;
                OnIsActiveChanged(new EventArgs());
                this.Invalidate();
            }
        }
        public event EventHandler IsActiveChanged;
        protected virtual void OnIsActiveChanged(EventArgs e)
        {
            if (IsActiveChanged != null)
                IsActiveChanged(this, e);
        }
        /// <summary>
        /// 彩色的
        /// </summary>
        bool isColor = false;
        public bool IsColor
        {
            get { return isColor; }
            set
            {
                if (isColor == value)
                    return;

                isColor = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 采用覆层的模式显示Hover状态
        /// </summary>
        HoverMode hoverMode = HoverMode.CoverColor;
        public HoverMode HoverMode
        {
            get { return hoverMode; }
            set
            {
                if (hoverMode == value)
                    return;

                hoverMode = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rect = e.ClipRectangle;
            //头像
            Image face;
            string name = "";
            if (displayItem == null)
            {
                name = "佚名";
                face = EmptyFace;
            }
            else
            {
                name = displayItem.Author;
                face = displayItem.FaceImage;
                if(name == null || name.Trim() == "") name = "佚名";
                if(face == null) face = EmptyFace;
            }

            Graphics g = e.Graphics;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            //背景色
            //using (Brush brush = new SolidBrush(backColor))
            //    g.FillRectangle(brush, rect);

            //图片
            Size faceSize = new Size(48,48);
            Rectangle faceRect = new Rectangle(rect.X + (rect.Width - faceSize.Width) / 2, rect.Y + 16, faceSize.Width, faceSize.Height);
            //获得图片中最大的正方形
            int width = Math.Min(face.Width, face.Height);
            if (width < 48) width = 48;
            Rectangle imageSrcRect=new Rectangle((face.Width-width)/2,(face.Height-width)/2,width,width);

            if (isColor)//彩色
                g.DrawImage(face, faceRect, imageSrcRect, GraphicsUnit.Pixel);
            else
            {//特殊效果
                switch (hoverMode)
                {
                    case HoverMode.Nothing:
                        g.DrawImage(face, faceRect, imageSrcRect, GraphicsUnit.Pixel);
                        break;
                    case HoverMode.CoverColor://附上半透明的涂层
                        g.DrawImage(face, faceRect, imageSrcRect, GraphicsUnit.Pixel);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(100, backColor)), faceRect);
                        break;
                    case HoverMode.BlankAndWhite://绘制为黑白图片
                        using (Bitmap bmp = ImageProcesser.ToGray(face))
                            g.DrawImage(bmp, faceRect, imageSrcRect, GraphicsUnit.Pixel);
                        break;
                }
            }

            //外边
            Rectangle outsideRect = new Rectangle(faceRect.X - 2, faceRect.Y - 2, faceRect.Width + 2 * 2, faceRect.Height + 2 * 2);
            //内边
            Rectangle insideRect = faceRect;

            Color color = borderColor;
            if (isActive)
                color = hoverBorderColor;
            //边框
            using (Brush brush = new SolidBrush(color))
            using (GraphicsPath path = Common.GetPath(outsideRect, 4))
            {
                path.FillMode = FillMode.Alternate;
                path.AddRectangle(faceRect);
                g.FillPath(brush, path);
                //g.FillRectangle(brush, rect);
            }

            color = borderColor;
            //显示名字
            //if (false)
            {
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                Rectangle nameRect = new Rectangle(rect.X, faceRect.Bottom + 5, rect.Width, 30);
                using (StringFormat format = new StringFormat(StringFormatFlags.LineLimit))
                {
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Near;
                    using (GraphicsPath path = new GraphicsPath(FillMode.Alternate))
                    {
                        path.AddString(name, base.Font.FontFamily, (int)base.Font.Style, base.Font.Size, nameRect, format);
                        g.DrawPath(Pens.White, path);
                        g.FillPath(new SolidBrush(Color.Black), path);

                        if (borderWidth > 0)
                        {//描边
                            GraphicsPath pathBorder = path.Clone() as GraphicsPath;
                            pathBorder.Widen(new Pen(color, borderWidth), null, flatness);
                            g.FillPath(new SolidBrush(color), pathBorder);
                            g.DrawPath(new Pen(color, 1), pathBorder);
                        }
                        using (Brush textBrush = new SolidBrush(foreColor))//this.ForeColor))
                        {
                            //g.DrawPath(new Pen(this.ForeColor), path);
                            g.FillPath(textBrush, path);
                        }
                    }
                }
            }
            this.realRect = outsideRect;

            base.OnPaint(e);
        }
    }
}
